package org.dtrd.modules.evaluation.controller;

import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.dtrd.base.BaseController;
import org.dtrd.common.api.vo.Result;
import org.dtrd.modules.base.service.BaseCommonService;
import org.dtrd.modules.evaluation.constant.Evaluation;
import org.dtrd.modules.evaluation.entity.bean.SasInfo;
import org.dtrd.modules.evaluation.entity.dto.SasDTO;
import org.dtrd.modules.evaluation.entity.po.DtrdEntEvaluationRdSas;
import org.dtrd.modules.evaluation.service.IDtrdEntEvaluationRdSasService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static org.dtrd.common.constant.CommonConstant.*;
import static org.dtrd.config.system.SystemState.ResponseState.*;

/**
 * @Author: QR
 * @Date: 2021/9/27-12:23
 */
@Api(tags = "量表评估")
@RestController
public class SasController extends BaseController {

    @Autowired
    private IDtrdEntEvaluationRdSasService sasService;
    @Autowired
    private BaseCommonService baseCommonService;

    @ApiOperation("保存sas(焦虑评估)量表记录")
    @PostMapping(value = {"/105/42/1054201", "/100/17/1001710"})
    public Result<?> saveSASInfo(@RequestBody SasDTO dto){
        if (ArrayUtil.isEmpty(dto.getAnswers())) {
            return Result.error(STATE_PARAMS_MISSING.getCode(), STATE_PARAMS_MISSING.getMsg());
        }
        if (dto.getAnswers().length != Evaluation.ProblemNum.SAS.getNum()
                || ArrayUtil.hasEmpty((Object) dto.getAnswers())) {
            return Result.error(STATE_PARAMS_ERROR.getCode(), STATE_PARAMS_ERROR.getMsg());
        }
        SasInfo info = sasService.saveSasInfo(dto);
        String logContent = String.format("保存sas(焦虑评估)量表记录，添加用户id：%d", dto.getPatientId());
        baseCommonService.addLog(logContent, OPERATE_TYPE_ADD);
        return Result.OK(info);
    }

    @ApiOperation("获取最近一次的sas(焦虑评估)量表记录-通过patientId")
    @GetMapping("/105/42/1054202")
    public Result<?> getSASInfoLatest(@RequestParam Integer patientId){
        if (patientId == null) {
            return Result.error(STATE_PARAMS_MISSING.getCode(), STATE_PARAMS_MISSING.getMsg());
        }
        if (patientId <= 0) {
            return Result.error(STATE_PARAMS_ERROR.getCode(), STATE_PARAMS_ERROR.getMsg());
        }
        SasInfo sasInfo = sasService.getSasInfoLatest(patientId);
        String logContent = String.format("获取最近一次的sas(焦虑评估)量表记录，查询用户id：%d", patientId);
        baseCommonService.addLog(logContent, OPERATE_TYPE_QUERY);
        return Result.OK(sasInfo);
    }


    @ApiOperation("获取sas(焦虑评估)量表记录列表")
    @GetMapping("/105/42/1054203")
    public Result<?> getSASInfoList(@RequestParam Integer patientId){
        if (isIntParamInvalid(patientId)){
            return error(STATE_PARAMS_MISSING);
        }
        LambdaQueryWrapper<DtrdEntEvaluationRdSas> queryWrapper = Wrappers.lambdaQuery(DtrdEntEvaluationRdSas.class)
                .eq(DtrdEntEvaluationRdSas::getPatientId, patientId)
                .orderByDesc(DtrdEntEvaluationRdSas::getCreateTime);
        List<DtrdEntEvaluationRdSas> poList = sasService.list(queryWrapper);
        List<SasInfo> dtoList = new ArrayList<>(poList.size());
        if (!poList.isEmpty()){
            dtoList = poList.stream()
                    .map(po -> new SasInfo().parseFromPo(po))
                    .collect(Collectors.toList());
        }
        String logContent = String.format("获取sas(焦虑评估)量表记录列表，查询用户id：%d", patientId);
        baseCommonService.addLog(logContent, OPERATE_TYPE_QUERY);
        return Result.OK(dtoList);
    }

    @ApiOperation("删除sas(焦虑评估)量表记录")
    @DeleteMapping("/105/42/1054204")
    public Result<?> removeSASInfoById(@RequestParam Integer dataId){
        if (isIntParamInvalid(dataId)){
            return error(STATE_PARAMS_MISSING);
        }
        boolean result = sasService.removeById(dataId);
        String logContent = String.format("删除sas(焦虑评估)量表记录，删除记录id：%d", dataId);
        baseCommonService.addLog(logContent, OPERATE_TYPE_DELETE);
        return result ? Result.OK() : error(STATE_OPERATION_FAILURE);
    }
}
